package cn.com.sina.finance.todolist.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import cn.com.sina.finance.todolist.R;
import cn.com.sina.finance.todolist.activity.LoginActivity;
import cn.com.sina.finance.todolist.base.BaseFragment;
import cn.com.sina.finance.todolist.bean.ApiResponse;
import cn.com.sina.finance.todolist.bean.UserInfo;
import cn.com.sina.finance.todolist.net.HttpCallback;
import cn.com.sina.finance.todolist.net.HttpUtil;
import cn.com.sina.finance.todolist.util.DimenUtil;
import cn.com.sina.finance.todolist.util.ToastUtil;

/**
 * @author yuebo
 * @date 2024/10/21
 */
public class UserFragment extends BaseFragment {

    private ImageView ivAvatar;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user;
    }

    @Override
    public void initView() {
        ivAvatar = viewHolder.getView(R.id.ivAvatar);
    }

    @Override
    public void initListener() {
        viewHolder.setOnClickListener(R.id.btnLogout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
            }
        });
        viewHolder.setOnClickListener(R.id.btnLogin, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), LoginActivity.class));
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    protected void getUserInfo() {
        String url = "http://chenyuebo.cn/login/userInfo";
        HttpUtil.get(url, new HttpCallback() {
            @Override
            public void onSuccess(String data) {
                ApiResponse<UserInfo> response = new Gson().fromJson(data, new TypeToken<ApiResponse<UserInfo>>() {
                }.getType());
                UserInfo userInfo = null;
                if (response.code == 0) {
                    userInfo = response.data;
                    Log.d("todo", "获取用户信息成功 userName=" + userInfo.userName);
                } else {
                    ToastUtil.showLongToast(getContext(), "获取用户信息失败:" + response.msg);
                }
                updateUserInfo(userInfo);
            }

            @Override
            public void onFail(int code, String msg, Throwable e) {
                ToastUtil.showLongToast(getContext(), "网络请求失败" + e.getMessage());
            }
        });
    }


    private void logout() {
        String url = "http://chenyuebo.cn/login/logout";
        HttpUtil.get(url, new HttpCallback() {
            @Override
            public void onSuccess(String data) {
                ApiResponse response = new Gson().fromJson(data, ApiResponse.class);
                if (response.code == 0) {
                    ToastUtil.showLongToast(getContext(), response.msg);
                    updateUserInfo(null);
                } else {
                    ToastUtil.showLongToast(getContext(), "获取用户信息失败:" + response.msg);
                }
            }

            @Override
            public void onFail(int code, String msg, Throwable e) {
                ToastUtil.showLongToast(getContext(), "网络请求失败" + e.getMessage());
            }
        });
    }

    private void updateUserInfo(@Nullable UserInfo userInfo) {
        if (userInfo == null) {
            // 未登录
            viewHolder.setVisible(R.id.layoutLogout, false);
            viewHolder.setVisible(R.id.layoutLogin, true);
        } else {
            // 已登录
            viewHolder.setVisible(R.id.layoutLogout, true);
            viewHolder.setVisible(R.id.layoutLogin, false);
            viewHolder.setText(R.id.tvUserName, "已登录:" + userInfo.userName);
            Glide.with(this).load(userInfo.userAvatar)
                    .transform(new RoundedCorners(DimenUtil.dp2px(getContext(), 25))) // 设置图片显示为圆角图片
                    .into(ivAvatar);
        }
    }
}
